#!/bin/sh

set -o errexit

if [ -f "/usr/libexec/os-helpers-logging" ]; then
	# shellcheck disable=SC1091
	. /usr/libexec/os-helpers-logging
else
	log () {
		_level="$1"
		_message="$2"
		printf "[%s][%s] %s\n" "$ME" "$_level" "$_message" >&2
		echo "$@"
	}
	fail () {
		error "$1"
		exit 1
	}

	error () {
		log "ERROR" "$1"
	}

	warn () {
		log "WARN" "$1"
	}

	info () {
		log "INFO" "$1"
	}
fi
# shellcheck disable=SC1091
[ -f "/usr/sbin/balena-config-defaults" ] && . /usr/sbin/balena-config-defaults

old_os_before_hooks=0
old_os_after_hooks=0
forward_hooks=1
commit_hooks=0
forward_cleanup=0

DURING_UPDATE=${DURING_UPDATE:-0}
HOOKS_DIR="/etc/hostapp-update-hooks.d/"
export DURING_UPDATE
export HOOKS_DIR

help () {
    cat << EOF
Script for setting balena shell environment
balena-config-vars [options]

Options:
	-a, --after
		Run 'old_os_after_hooks' hooks. Usually run in old os environment and after forward hooks
		Default: After hooks are not run.
	-b, --before
		Run 'old_os_before_hooks' hooks. Usually run in old os environment and before forward hooks
		Default: Before hooks are not run.
	-c, --commit:
		Run in new OS. Confirm changes by forward hooks.
		Default: Commit hooks are not run.
	-d, --dir=HOOKS_DIR:
		Path to hooks directory. Useful when running old_os_before_hooks and old_os_after_hooks
	-f, --full:
		Full hooks, Run before,forward and after. But in same environment.
	-h, --help
		Display this help and exit.
EOF
}

DEBUG="0"
if [ -n "${CONFIG_PATH}" ]; then
	DEBUG=$(jq -r '.debug // empty' "${CONFIG_PATH}")
fi
run_hook () {
	if [ "${DEBUG}" = "1" ]; then
		/bin/sh -x "$1"
	else
		"$1"
	fi
}

# Parse arguments
while [ "$#" -gt "0" ]; do
	key=$1
	case $key in
		-a|--after)
			old_os_after_hooks=1
			forward_hooks=0
			;;
		-b|--before)
			old_os_before_hooks=1
			forward_hooks=0
			;;
		-c|--commit)
			commit_hooks=1
			forward_hooks=0
			;;
		-d|--dir)
			HOOKS_DIR="$2"
			shift
			;;
		-f|--full)
			old_os_before_hooks=1
			forward_hooks=1
			old_os_after_hooks=1
			;;
		-h|--help)
			help
			exit 0
			;;
		*)
			warn "Argument '$1' unknown. Ignoring."
			;;
	esac
    shift
done

# first 'find' is for v1 hooks. maxdepth 1 lists all files in HOOKS_DIR. next find is for 'forward' v2 hooks.
list_of_forward_hooks=$({ find "${HOOKS_DIR}" -maxdepth 1 -type f ; find "${HOOKS_DIR}" -type f | grep forward ; } | sort)
list_of_forward_cleanup_hooks=$(find "${HOOKS_DIR}" -type f | grep cleanup | sort)
list_of_forward_commit_hooks=$(find "${HOOKS_DIR}" -type f | grep commit | sort)
list_of_old_os_before_hooks=$(find "${HOOKS_DIR}" -type f | grep before | sort)
list_of_old_os_after_hooks=$(find "${HOOKS_DIR}" -type f | grep after | sort)

if [ "$old_os_before_hooks" = "1" ] ; then
	for hook in $list_of_old_os_before_hooks; do
		run_hook "$hook"
	done
fi

if [ "$forward_hooks" = "1" ] ; then
	for hook in $list_of_forward_hooks; do
		if ! run_hook "$hook" ; then
			forward_cleanup=1
			break
		fi
	done
fi

if [ "$old_os_after_hooks" = "1" ] ; then
	for hook in $list_of_old_os_after_hooks; do
		run_hook "$hook"
	done
fi

if [ "$commit_hooks" = "1" ] ; then
	for hook in $list_of_forward_commit_hooks; do
		run_hook "$hook"
	done
fi

if [ "$forward_cleanup" = "1" ] ; then
	for hook in $list_of_forward_cleanup_hooks; do
		run_hook "$hook"
	done
	exit 1
fi
